
#include "Particle.hpp"



void ParticleProjectileEmitter::createBuffer() {
	cout << "creating buffer for ParticleProjectileEmitter" << endl; // DEBUG
	cout << "m_limits vaut : " << m_limits << endl;
	// attrib location
	glBindAttribLocation(m_material->getShaderProgram()->handle(),DIRECTION_ATTR,"PPDirection");
	glBindAttribLocation(m_material->getShaderProgram()->handle(),START_TIME_ATTR,"startTime");
	glBindAttribLocation(m_material->getShaderProgram()->handle(),INITIAL_SPEED_ATTR,"initialSpeed");

	glLinkProgram(m_material->getShaderProgram()->handle());
	glValidateProgram(m_material->getShaderProgram()->handle());

	// uniform location
	int handle_ = m_material->getShaderProgram()->handle();

	m_globalTime_id 		= glGetUniformLocation(handle_, "globalTime");
	m_initialPosition_id 	= glGetUniformLocation(handle_, "initialPosition");
	m_cameraPosition_id 	= glGetUniformLocation(handle_, "cameraPosition");
	m_alphaDecrease_id 		= glGetUniformLocation(handle_, "alphaDecrease");
	m_sizeUpdate_id 		= glGetUniformLocation(handle_, "sizeUpdate");
	m_initialPointSize_id 	= glGetUniformLocation(handle_, "initialPointSize");
	m_MVPMatrix_id 			= glGetUniformLocation(handle_, "MVPMatrix");

	GLfloat* direction 		= new GLfloat[3 * m_limits];
	GLfloat* initialSpeed 	= new GLfloat[m_limits];
	GLfloat* startTime 		= new GLfloat[m_limits];
	//cout << "direction declaration 2" << endl;
	for (int i = 0; i < m_limits; i++) {
		// direction declaration
		const int amplitude = 1000;
		//const int amplitude_h = 500; // half
		//float amplitude_f = amplitude;
		glm::vec3 tmp;
		//tmp.x = ((rand() % amplitude) - amplitude_h) / amplitude_f;
		//tmp.y = ((rand() % amplitude) - amplitude_h) / amplitude_f;
		//tmp.z = ((rand() % amplitude) - amplitude_h) / amplitude_f;
		tmp = m_direction;

		//glm::vec3 tmp(((rand() % 1000) - 500) / 1000.0,((rand() % 1000) - 500) / 1000.0,((rand() % 1000) - 500) / 1000.0);
		//tmp = (m_direction + 0.5f * tmp);
		direction[3*i]   = tmp.x;
		direction[3*i+1] = tmp.y;
		direction[3*i+2] = tmp.z;
		// initial speed declaration
		float accTime = startTime[i] = i / ((float) pack);
		initialSpeed[i] = exp(- 20.0 * (accTime)) * m_speedIntensity * ( ((rand() %1000) - 500) / 2000.0 + 1.0 );
	}
	for (int i = 0; i < m_limits; i++) {
		glm::vec3 tmp(direction[3*i], direction[3*i+1], direction[3*i+2]);
		//cout << tmp.x << " " << tmp.y << " " << tmp.z << endl;
	};
	// buffer filling
	glGenBuffers(3,m_buffers);
	// direction buffer
	glBindBuffer(GL_ARRAY_BUFFER,m_buffers[DIRECTION_ARRAY]);
	glBufferData(GL_ARRAY_BUFFER,3 * sizeof(GLfloat)*m_limits,direction,GL_STATIC_DRAW);
	// speed buffer
	glBindBuffer(GL_ARRAY_BUFFER,m_buffers[INITIAL_SPEED_ARRAY]);
	glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat) * m_limits,initialSpeed,GL_STATIC_DRAW);
	// start time array
	glBindBuffer(GL_ARRAY_BUFFER,m_buffers[START_TIME_ARRAY]);
	glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat) * m_limits,startTime,GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER,0);
};
